<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入验证码
use think\captcha\Captcha;

//引入数据库
use think\Db;

class Index extends Backend
{
    //覆盖全局属性 不需要登录
    protected $NoNeedLogin = ['login', 'captcha', 'logout'];

    public function __construct(){

        //继承父类
        parent::__construct();
        //创建公共的属性
        $this->AdminModel = model('Admin');
    }

    public function index()
    {

        // return '这是后台';
        return $this->fetch();
    }

    // 主页
    public function main()
    {
        $UserCount = Db::table('pre_user')->count();
        $PostCount = Db::table('pre_post')->count();
        $CommentCount = Db::table('pre_comment')->count();
        $RechargeCount = Db::table('pre_user_record')->where(['state' => '3'])->count();

        //查询数据库版本
        $version = Db::query("select version() as ver");

        $this->assign([
            'UserCount' => $UserCount,
            'PostCount' => $PostCount,
            'CommentCount' => $CommentCount,
            'RechargeCount' => $RechargeCount,
            'MysqlVersion' => $version[0]['ver']
        ]);

        return $this->fetch();
    }

    // 退出
    public function logout(){

        // 删除session
        session('LoginAdmin', null);

        $this->success('退出成功', url('admin/index/login'));
    }

    //登陆
    public function login()
    {

        //判断是否有登录
        $LoginAdmin = session('LoginAdmin');

       if(!empty($LoginAdmin))
       {

            //查出来是否正确
            $id = isset($LoginAdmin['id']) ? $LoginAdmin['id'] : 0;

            $admin = $this->AdminModel->find($id);

            //当这个人不存在
            if(!$admin)
            {
                //session属于伪造的   // 删除（当前作用域）
                session('LoginAdmin', null);
                $this->error('非法登录', url('admin/index/login'));
                exit;
            }

            //判断是否有禁用
            if($admin['state'] == 0)
            {
                session('LoginAdmin', null);
                $this->error('该管理员已被禁用', url('admin/index/login'));
                exit;
            }

            $this->success('已经登陆不需要重复登陆', url('admin/index/index'));
            exit;
        }


        if($this->request->isPost()){

            $params = $this->request->param();
            $vercode = $this->request->param('vercode');

            //判断验证码是否正确 如果结果返回true就是正确否则失败
            if(!captcha_check($vercode)){
                $this->error('验证码输入错误');
                exit;
            }

            $admininfo = $this->AdminModel->where(['username' => $params['username']])->find();

            //判断是否查询到
            if(!$admininfo)
            {
                $this->error('管理员不存在');
                exit;
            }

            //密码盐
            $salt = $admininfo['salt'];

            // 密码加密
            $password = md5($params['password'].$salt);

            if($password != $admininfo['password'])
            {
                $this->error('管理员密码错误');
                exit;
            }

            //判断账号是否被禁用
            if($admininfo['state'] == 0){

                $this->error('该账号已被禁用');
                exit;
            }

            $data = [
                'id' => $admininfo['id'],
                'nickname'=> $admininfo['nickname']
            ];

            //设置session
            session('LoginAdmin', $data);

            $this->success('管理员登录成功', url('admin/index/index'));
            exit;
        }

        return $this->fetch();
    }

    // 生成验证码
    public function captcha(){
        $config =    [
            // 验证码位数
            'length'=> 4
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }


    
    // 配置列表页
    public function undefined()
    {
        $count = $this->AdminModel->count();

        $countlist = $this->AdminModel->paginate(10);

        $pagestr = $countlist->render();

        // var_dump($countlist);
        // exit;

        $this->assign([
            'count' => $count,
            'countlist' => $countlist,
            'pagestr' => $pagestr
        ]);

        return $this->fetch();
    }

    
}
